package com.haizhi.crm.persist.service.impl;

import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.persist.constant.PersistTarget;
import com.haizhi.crm.persist.manager.EsPersistManager;
import com.haizhi.crm.persist.model.PersistReq;
import com.haizhi.crm.persist.model.PersistResp;
import com.haizhi.crm.persist.service.PersistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * Created by chengmo on 2018/6/19.
 */
@Service
public class PersistServiceImpl implements PersistService {
    private static final GLog LOG = LogFactory.getLogger(PersistServiceImpl.class);

    @Autowired
    private EsPersistManager esPersistManager;

    @Override
    public boolean bulkPersist(PersistReq request) {
        Set<PersistTarget> targets = request.getTargets();
        if (targets.isEmpty()) {
            LOG.warn("targets is empty" );
            return true;
        }
        boolean success = true;
        // 先简单处理
        for (PersistTarget target : targets) {
            switch (target) {
                case ES:
                    PersistResp resp = esPersistManager.bulkPersist(request.getRequestApi());
                    success = resp.isSuccess();
                    break;
            }
        }
        return success;
    }
}
